package com.framework.plm.bas.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.framework.plm.bas.bs.IPlmBasBS;
import com.framework.plm.bas.web.controller.PageSupport;

public class UtilWeb {
	
	private static IPlmBasBS plmBasBS = null;
	private static final Log log = LogFactory.getLog(UtilWeb.class);

	protected UtilWeb() {
	}
	
	public static IPlmBasBS getPlmBasBS() {
		if (plmBasBS == null) {
			plmBasBS = (IPlmBasBS) SpringUtil.getBean(IPlmBasBS.BEAN_ID);
		}
		return plmBasBS;
	}
	
	/**
	 * 取得默认方式提交的交易码。
	 * @param request 请求上下文
	 * @return
	 */
	public static String getServiceCode(HttpServletRequest request) {
		String str = null;
		if (StringUtils.isNotBlank(request.getParameter(PlmBasConstant.APP_SERVICE))) {
			str = request.getParameter(PlmBasConstant.APP_SERVICE).toString();
		}
		return str;
	}
	
	/**
	 * 封装JSON请求参数
	 * @param request 请求上下文
	 * @return PageSupport
	 */
	public static PageSupport transJsonParam(HttpServletRequest request) {
	    PageSupport	ps = new PageSupport();
	    String tmp = request.getParameter(PlmBasConstant.REQ_JSON_CURPAGENO)==null?null : request.getParameter(PlmBasConstant.REQ_JSON_CURPAGENO);
		if(StringUtils.isBlank(tmp)){
			ps.setCurPageNo(0);
		}else{
			ps.setCurPageNo(Integer.parseInt(tmp));
		}
		tmp =  request.getParameter(PlmBasConstant.REQ_JSON_PAGESIZE)==null?null :  request.getParameter(PlmBasConstant.REQ_JSON_PAGESIZE);
		if(StringUtils.isBlank(tmp)){
			ps.setPageSize(getPlmBasBS().queryPageSize());
		}else{
			ps.setPageSize(Integer.parseInt(tmp));
		}
		tmp = request.getParameter(PlmBasConstant.REQ_JSON_TOTALPAGE)==null?null : request.getParameter(PlmBasConstant.REQ_JSON_TOTALPAGE);
		if(StringUtils.isBlank(tmp)){
			ps.setTotalPage(0);
		}else{
			ps.setTotalPage(Integer.parseInt(tmp));
		}
		tmp = request.getParameter(PlmBasConstant.REQ_JSON_TOTALRECORD)==null?null : request.getParameter(PlmBasConstant.REQ_JSON_TOTALRECORD).toString();
		if(StringUtils.isBlank(tmp)){
			ps.setTotalRecord(0);
		}else{
			ps.setTotalRecord(Integer.parseInt(tmp));
		}
		return ps;
	}
	
	/**
	 * 获取系统标记
	 * @return String
	 */
	public static String getSysFlag() {
		URL path = Thread.currentThread().getContextClassLoader().getResource("/config/prop/app.properties");
		InputStream source = null;
		Properties prop = new Properties();
		String sysFlag = null;
		try{
			source = path.openStream();
			if(source!=null){
				prop.load(source);
				sysFlag = prop.getProperty("app.sys");
				prop.clear();
			}else{
				log.error("系统标志配置读取失败");
			}
		}catch(IOException e1){
			log.error("系统标志配置读取失败");
		}catch(Exception e){
			log.error("系统标志配置读取失败");
		}finally{
			try{
				if(source!=null){
					source.close();
				}
			}catch(IOException e2){}			
		}		
		return sysFlag;
	}
	
	/**
	 * 获取系统信息数组
	 * @return String
	 */
	public static Map<String, String> getSysInfoArray() {
		URL path = Thread.currentThread().getContextClassLoader().getResource("/config/prop/app.properties");
		InputStream source = null;
		Properties prop = new Properties();
		Map<String, String> sysInfo = new HashMap<String, String>();
		try{
			source = path.openStream();
			if(source!=null){
				prop.load(source);
				for(Object keys : prop.keySet()){
					sysInfo.put(keys.toString(), prop.getProperty(keys.toString()));
				}
				prop.clear();
			}else{
				log.error("系统信息数组读取失败");
			}
		}catch(Exception e1){
			log.error("系统信息数组读取失败");
		}finally{
			try{
				if(source!=null){
					source.close();
				}
			}catch(IOException e2){}			
		}		
		return sysInfo;
	}
	
}
